package com.heima.xiyuanmall.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.heima.mvplibrary.base.BaseActivity;
import com.heima.mvplibrary.base.BaseAsyncTask;
import com.heima.mvplibrary.base.BasePresenter;
import com.heima.xiyuanmall.MainActivity;
import com.heima.xiyuanmall.R;
import com.heima.xiyuanmall.presenter.SplashPresenter;
import com.heima.xiyuanmall.view.LaunchTextView;
import com.orhanobut.logger.Logger;


/**
 *  @author Jins
 *  @date created at：2021/9/4
 *  @Description: 引导页
 */
public class SplashActivity extends BaseActivity<SplashPresenter> {

    private static final int PERMISSIONS_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setContentView(R.layout.activity_splash);


    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData() {
//        AssetManager assetManager = getAssets();
//        ((TextView)findViewById(R.id.tv_appSplash)).setTypeface(Typeface.createFromAsset(assetManager,"fonts/HYShangWeiShouShuW.ttf"));


        ((LaunchTextView)findViewById(R.id.tv_appSplash)).startAnimation();

//        //实例化TextView
//        TextView textView = findViewById(R.id.tv_appSplash);
////得到AssetManager
//        AssetManager mgr=getApplicationContext().getAssets();
////根据路径得到Typeface
//        Typeface tf=Typeface.createFromAsset(mgr, "fonts/HYShangWeiShouShuW.ttf");
////设置字体
//        textView.setTypeface(tf);


        //请求权限
        ActivityCompat.requestPermissions(
                this,
                getP().permissions,
                PERMISSIONS_CODE
        );

        BaseAsyncTask asyncTask = new BaseAsyncTask();
        asyncTask.execute("开始运行！");

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Logger.i("aaaaa");
                asyncTask.cancel(true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        asyncTask.cancel(true);
                    }
                }, 1000);
            }
        }, 2000);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSIONS_CODE) {
            for (int i = 0; i < grantResults.length; i++) {
                Log.d("TAG", "onRequestPermissionsResult: "+grantResults[i]);
            }
            for (String permission : getP().permissions) {
                int result = ContextCompat.checkSelfPermission(this, permission);
                if (result != PackageManager.PERMISSION_GRANTED) {
                    finish();
                    return;
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            overridePendingTransition(R.anim.anim_in, R.anim.anim_out);

        }
    }
}